package com.kgc.dao;

import com.kgc.entity.Payment;
import com.kgc.untils.PageUtils;
import org.apache.commons.lang3.StringUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PaymentDao extends BaseDao {


    /**
     * 查所有
     *
     * @return
     */
    public List<Payment> selectAll(PageUtils pageUtils, String name) {
        List<Payment> list = new ArrayList<>();
        try {
            String sql = "select * from payment ";
            if (StringUtils.isNotEmpty(name)) {
                sql += " WHERE status like '%" + name + "%'";
            }
            sql += " limit ?,?";
            ResultSet rs = super.getExecuteQuery(sql, pageUtils.start(), pageUtils.getPageSize());
            while (rs.next()) {
                list.add(new Payment(rs.getInt(1), rs.getString(2)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return list;

    }

    /**
     * 查总数
     *
     * @return
     */
    public int total(String name) {
        try {
            String sql = "select count(*) from payment";
            if (StringUtils.isNotEmpty(name)) {
                sql += " WHERE status like '%" + name + "%'";
            }
            ResultSet rs = super.getExecuteQuery(sql);
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }

    /**
     * 增加
     *
     * @param payment
     * @return
     */
    public int addpayment(Payment payment) {
        String sql = "INSERT INTO `payment`( `name`) VALUES  (?)";
        return super.getExecuteUpdate(sql, payment.getName());
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public int deletePayment(String id) {
        String sql = "delete from payment where id =?";
        return super.getExecuteUpdate(sql, id);
    }

    /**
     * 修改
     *
     * @param payment
     * @return
     */
    public int editPayment(Payment payment) {
        String sql = "UPDATE `payment` SET `name` = ? WHERE `id` = ?";
        return super.getExecuteUpdate(sql, payment.getName(), payment.getId());

    }

    /**
     * 查询所有收费类型
     *
     * @return
     */
    public List<Payment> selectPayment() {
        List<Payment> list = new ArrayList<>();
        try {
            String sql = "SELECT * FROM payment";
            ResultSet rs = super.getExecuteQuery(sql);
            while (rs.next()) {
                list.add(new Payment(rs.getInt(1),rs.getString(2)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return list;
    }
}
